package frsf.cidisi.exercise.modelo.search.actions;

import frsf.cidisi.exercise.modelo.search.*;
import frsf.cidisi.exercise.modelo.util.Orientacion;
import frsf.cidisi.faia.agent.search.SearchAction;
import frsf.cidisi.faia.agent.search.SearchBasedAgentState;
import frsf.cidisi.faia.state.AgentState;
import frsf.cidisi.faia.state.EnvironmentState;

public class GirarDer extends SearchAction {

    /**
     * This method updates a tree node state when the search process is running.
     * It does not updates the real world state.
     */
    @Override
    public SearchBasedAgentState execute(SearchBasedAgentState s) {
        AgenteEstado agState = (AgenteEstado) s;
        
        Orientacion actual = agState.getOrientacion();
        agState.setOrientacion(this.girarDerecha(actual));
        
        return agState;
    }
    
    /* Norte = 1
	 * Sur = 2
	 * Este = 3
	 * Oeste = 4
	 */
    private Orientacion girarDerecha(Orientacion actual){
    	switch(actual.getValue()){
    	case 1:
    		return new Orientacion(3);
    	case 2:
    		return new Orientacion(4);
    	case 3:
    		return new Orientacion(2);
    	case 4:
    		return new Orientacion(1);
    	}
    	return actual;
    }

	
    /**
     * This method updates the agent state and the real world state.
     */
    @Override
    public EnvironmentState execute(AgentState ast, EnvironmentState est) {
        AmbienteEstado environmentState = (AmbienteEstado) est;
        AgenteEstado agState = ((AgenteEstado) ast);

        Orientacion actual = agState.getOrientacion();
        agState.setOrientacion(this.girarDerecha(actual));

        return environmentState;
    }

    /**
     * This method returns the action cost.
     */
    @Override
    public Double getCost() {
        return new Double(3);
    }

    /**
     * This method is not important for a search based agent, but is essensial
     * when creating a calculus based one.
     */
    @Override
    public String toString() {
        return "GirarDer";
    }
}